/*
 * Copyright (c) 2024 Black Sesame Technologies
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "dal_fastdds/utils/StringUtils.hpp"

uint32_t GetStringLengthInByteArray(octet* address)
{
    uint32_t len = 0;
    while (address[len] != 0) {
        len++;
    }
    return len;
}

void trim(std::string& s)
{
    int index = 0;
    if (s.empty())
        return;

    while ((index = s.find(' ', index)) != std::string::npos) {
        s.erase(index, 1);
    }
}

std::vector<std::string> Split(std::string s, const char ch)
{
    int start = 0;
    int len = 0;
    std::vector<std::string> ret;

    trim(s);
    for (int i = 0; i < s.length(); i++) {
        if (s[i] == ch) {
            ret.push_back(s.substr(start, len));
            start = i + 1;
            len = 0;
        }
        else {
            len++;
        }
    }
    if (start < s.length())
        ret.push_back(s.substr(start, len));
    return ret;
}